import React, {Component} from 'react'
import {fetchData} from '../../../components/AjaxConsumer'
import {
    Form,
    Input,
    Tooltip,
    Icon,
    Cascader,
    Select,
    Row,
    Col,
    Checkbox,
    Button,
    message,
    Modal,
    Radio,
    InputNumber
} from 'antd';
import { SelectUrl } from '../../../components';
const FormItem = Form.Item;

class ProModelForm extends React.Component {
    state = {
        loading: false,
        isEdit: false,
        submitUrl: '',
        unitLengthUrl:'/base/dic/get/unit',
        machineNumberUrl:'/base/dic/get/machine_number',
        formData: {}
    };

    handleSubmit = (e) => {
        e.preventDefault();
        console.log('提交表单handleSubmit------8888-----');
        this.props.form.validateFieldsAndScroll((err, fieldsValue) => {
            if (!err) {
                this.setState({loading: true});
                let values = {
                    ...fieldsValue,
                };
                console.log('表单提交数据-----------99999', values);
                //发送ajax 控制请求数据规则，无值的设为空
                for (let item in values) {
                    if (!values[item]) {
                        values[item] = ''
                    }
                }
                const options = {
                    url: this.state.submitUrl,
                    type: 'post',
                    condition: values
                };

                fetchData(
                    this,
                    options,
                    // (res) => {
                    //     //成功回调函数
                    //     this.setState({
                    //         loading: false
                    //     });
                    //     if (res.code == '1') {
                    //         // message.success('操作成功');
                    //         message.success(res.msg);
                    //         this.handleCancel();
                    //         this.props.refreshHandle();
                    //     } else {
                    //         // message.error("111");
                    //     }

                    // },
                    (res) => {
                        this.setState({
                            loading: false
                        });
                        if (res.code == '1') {                            
                            message.success(res.msg);
                            this.handleCancel();
                            this.props.refreshHandle();
                        } else {
                            Modal.error({
                                title: '错误提示',
                                content: resData.msg,
                            });
                        }

                    }
                )
            }
        });
    }

    handleCancel = () => {
        this.props.onCancel();//123
        this.props.form.resetFields(); //初始化表单
        //初始化ajax加载过来的selectList
    }

    checkOutAmountNumber = (rule, value, callback) => {
    const form = this.props.form;
    if (value && value>0) {
      callback();
    } else {
      callback('基本单位数量应为大于0的数字');
    }
  }

    checkInAmountNumber = (rule, value, callback) => {
    const form = this.props.form;
    if (value && value>0) {
      callback();
    } else {
      callback('转换数量应为大于0的数字');
    }
  }

    //propertity 修改时执行
    componentWillReceiveProps(newProps){
        this.setState({
            isEdit: newProps.isEdit,
            submitUrl: newProps.submitUrl,
            formData: newProps.formData
        });
    }

    // 组件参数实例化-仅一次
    componentWillMount(){
        this.setState({
            isEdit: this.props.isEdit,
            submitUrl: this.props.submitUrl,
            formData: this.props.formData
        });
    }

    render() {
        const {getFieldDecorator} = this.props.form;
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 6}
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 18}
            }
        };
        const tailFormItemLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 24, offset: 10}
            }
        };

        let objectInfo = this.state.formData;

        return (
            <div>
                <Form
                    onSubmit={this.handleSubmit}
                    style={{
                                backgroundColor: '#fff',
                                paddingTop: '20px',
                                paddingBottom: '20px'
                            }}>
                    <Row>
                        <Col span={0}>
                            <FormItem>
                                {getFieldDecorator('modelId', {
                                    initialValue: objectInfo.modelId ? objectInfo.modelId.toString() : "",
                                    rules: [
                                        {}
                                    ]
                                })(<Input type="hidden"/>)}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="品名">
                                {getFieldDecorator('productName', {
                                    initialValue: objectInfo.productName ? objectInfo.productName : "",
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请输入品名'
                                        }
                                    ]
                                })(
                                    <Input placeholder="请输入"/>
                                    // <SelectUrl url={this.state.modelTypeUrl} type="get" dicCode="dicCode" dicName="dicName"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="定量">
                                {getFieldDecorator('density', {
                                    initialValue: objectInfo.density ? objectInfo.density : "",
                                    rules: [
                                      {
                                          required: true,whitespace: true,message: '请输入定量'
                                        }
                                    ]
                                })(
                                    <Input placeholder="请输入"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="规格">
                                {getFieldDecorator('length', {
                                    initialValue: objectInfo.length ? objectInfo.length : "",
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请输入规格'
                                        }
                                    ]
                                })(
                                    <Input placeholder="请输入"/>
                                )}
                            </FormItem>
                        </Col>
                         <Col span={24}>
                            <FormItem {...formItemLayout} label="计量单位">
                                {getFieldDecorator('unit', {
                                    initialValue: objectInfo.unit ? objectInfo.unit : "",
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请输入计量单位'
                                        }
                                    ]
                                })(
                                    <SelectUrl url={this.state.unitLengthUrl} type="get" dicCode="dicCode" dicName="dicName"/>
                                )}
                            </FormItem>
                        </Col>
                         <Col span={24}>
                            <FormItem {...formItemLayout} label="生产机器号">
                                {getFieldDecorator('machineNumber', {
                                    initialValue: objectInfo.machineNumber ? objectInfo.machineNumber : "",
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请输入生产机器号'
                                        }
                                    ]
                                })(
                                    <SelectUrl url={this.state.machineNumberUrl} type="get" dicCode="dicCode" dicName="dicName"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24} style={{ textAlign: 'center',marginTop:'10px' }}>
                                <Button type="primary" htmlType="submit" size="large">
                                    {this.state.loading ? '提交中...' : '确定'}
                                </Button>
                                <Button type="default" style={{marginLeft: '50px'}} size="large" onClick={this.handleCancel}>
                                    取消
                                </Button>
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

export default Form.create()(ProModelForm);
